package com.pfsoft.codehelper.controller;

import com.pfsoft.codehelper.domain.StudentInfo;
import com.pfsoft.codehelper.service.IStudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.pfsoft.codehelper.common.response.BaseResponse;
import com.pfsoft.codehelper.common.query.StudentInfoQuery;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import com.pfsoft.codehelper.common.exception.BadRequestException;

import org.springframework.data.domain.Page;

/**
* @author pf
* @date 2020-06-12
*/
@Controller
public class StudentInfoController {

    @Autowired
    private IStudentInfoService studentInfoService;


    private static final String ENTITY_NAME = "studentInfo";

    /**
    * 返回页面
    *
    */
    @GetMapping(value = "/studentInfos")
    public String getStudentInfoIndex(){
        return "/admin/studentInfo";
    }

    /**
    * 按ID查询
    *
    */
    @GetMapping(value = "/api/studentInfo/{id}")
    public ResponseEntity<BaseResponse<StudentInfo>> getStudentInfo(@PathVariable Long id){
        BaseResponse<StudentInfo> baseResponse = new BaseResponse<>();
        StudentInfo studentInfo  = studentInfoService.findById(id);
        baseResponse.setCode(200);
        baseResponse.setMessage("success");
        baseResponse.setData(studentInfo);
        return new ResponseEntity<>(baseResponse, HttpStatus.OK);
    }

    /**
    * 按查询条件查询
    *
    */
    @PostMapping(value = "/api/studentInfos")
    public ResponseEntity<BaseResponse<Page<StudentInfo>>> getStudentInfos(@RequestBody StudentInfoQuery query){
        PageRequest pageRequest = PageRequest.of(query.getPage(), query.getSize(), new Sort(Sort.Direction.fromString(query.getOrder()), query.getSort()));
        BaseResponse<Page<StudentInfo>> baseResponse = new BaseResponse<>();
        Page<StudentInfo> all = studentInfoService.getAll(query, pageRequest);
        baseResponse.setCode(200);
        baseResponse.setMessage("success");
        baseResponse.setData(all);
        return new ResponseEntity<>(baseResponse, HttpStatus.OK);
    }

    /**
    * 创建
    *
    */
    @PostMapping(value = "/api/studentInfo")
    public  ResponseEntity<BaseResponse<Page<StudentInfo>>> create(@Validated @RequestBody StudentInfo resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setMessage("success");
        baseResponse.setCode(200);
        baseResponse.setData(studentInfoService.create(resources));
        return new ResponseEntity<>(baseResponse,HttpStatus.CREATED);
    }

    /**
    * 修改
    *
    */
    @PutMapping(value = "/api/studentInfo")
    public ResponseEntity update(@Validated @RequestBody StudentInfo resources){
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME +" ID Can not be empty");
        }
        studentInfoService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    /**
    * 删除
    *
    */
    @DeleteMapping(value = "/api/studentInfos/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        studentInfoService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}